<template>
  <div class="contain">
    <el-card style="margin: 20px">
      <!-- 精选题库 -->
      <!-- 提示文案 -->
      <el-alert type="warning" :closable="false" style="margin-bottom: 10px">
        <i class="el-icon-info" />说明：目前只支持学科和关键字条件筛选
      </el-alert>
      <!-- 表单区域，收集信息 -->
      <el-form label-width="100px">
        <!-- 第一行标签 -->
        <el-row :gutter="3">
          <el-col :span="6">
            <!-- 可搜索 -->
            <el-form-item label="学科✨" style="margin-bottom: 2px">
              <el-select
                v-model="searFrom.subjectID"
                size="small"
                placeholder="请选择学科"
              >
                <el-option
                  v-for="item in SubjectList"
                  :key="item.id"
                  :label="item.subjectName"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <!-- 可搜索 -->
            <el-form-item label="关键字✨" style="margin-bottom: 2px">
              <el-input
                v-model="searFrom.keyword"
                size="small"
                placeholder="请输入关键字"
              />
            </el-form-item>
          </el-col>
          <!-- 选择城市和区 -->
          <el-col :span="6">
            <el-form-item label="二级目录" style="margin-bottom: 2px">
              <el-select v-model="province" size="small">
                <el-option
                  v-for="item in Area"
                  :key="item.value"
                  :label="item.label"
                  :value="item"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="标签" style="margin-bottom: 2px">
              <el-select v-model="city" size="small">
                <el-option
                  v-for="item in cityQuery"
                  :key="item.value"
                  :label="item.label"
                  :value="item"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- 第二行标签 -->
        <el-row :gutter="3">
          <el-col :span="6">
            <el-form-item label="试题类型" style="margin-bottom: 2px">
              <el-input
                v-model="searFrom.tags"
                size="small"
                placeholder="请输入企业标签"
              />
            </el-form-item>
          </el-col>
          <!-- 选择城市和区 -->
          <el-col :span="6">
            <el-form-item label="难度" style="margin-bottom: 2px">
              <el-select v-model="province" size="small">
                <el-option
                  v-for="item in Area"
                  :key="item.value"
                  :label="item.label"
                  :value="item"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="方向" style="margin-bottom: 2px">
              <el-select v-model="city" size="small">
                <el-option
                  v-for="item in cityQuery"
                  :key="item.value"
                  :label="item.label"
                  :value="item"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="录入人" style="margin-bottom: 2px">
              <el-input
                v-model="searFrom.shortName"
                size="small"
                placeholder="请输入关键字"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <!-- 第三行表单框 -->
        <el-row :gutter="3">
          <el-col :span="6">
            <el-form-item label="题目备注" style="margin-bottom: 2px">
              <el-input
                v-model="searFrom.tags"
                size="small"
                placeholder="请输入题目备注"
              />
            </el-form-item>
          </el-col>
          <!-- 选择城市和区 -->
          <el-col :span="6">
            <el-form-item label="城市" style="margin-bottom: 2px">
              <el-select v-model="province" size="small">
                <el-option
                  v-for="item in Area"
                  :key="item.value"
                  :label="item.label"
                  :value="item"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="地区" style="margin-bottom: 2px">
              <el-select v-model="city" size="small">
                <el-option
                  v-for="item in cityQuery"
                  :key="item.value"
                  :label="item.label"
                  :value="item"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <!-- 第四行标签 -->
        <el-row>
          <el-form-item label="企业简称">
            <el-col :span="18">
              <el-input
                v-model.trim="searFrom.state"
                style="width: 200px; margin-right: 15px"
                placeholder="请输入企业简称"
                size="small"
              />

              <el-button size="small" @click="handelMationClean">
                清空
              </el-button>
              <el-button type="primary" size="small" @click="getDatabaseList">
                搜索
              </el-button>
            </el-col>
            <el-col :span="6" style="text-align: right">
              <el-button
                type="success"
                class="right"
                size="small"
                @click="$router.push(`/list/card`)"
              >
                新增试题
              </el-button>
            </el-col>
          </el-form-item>
        </el-row>
      </el-form>
      <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
        <el-tab-pane label="全部" name="first" />
        <el-tab-pane label="待审核" name="second" />
        <el-tab-pane label="已审核" name="third" />
        <el-tab-pane label="已拒绝" name="fourth" />
      </el-tabs>
      <!-- 提示文案 -->
      <el-alert type="info" :closable="false">
        <i class="el-icon-info" />共{{ total }}条记录</el-alert
      >
      <!-- 表单区域 -->
      <el-table :data="DatabaseList">
        <el-table-column label="试题编号" prop="number" width="150" />
        <el-table-column label="学科" prop="subject" />
        <el-table-column label="目录" prop="cataloglD" />
        <el-table-column label="题型" prop="questionType">
          <template #default="{ row }">
            {{ row.questionType === 1 ? '单选' : '多选' }}
          </template>
        </el-table-column>
        <el-table-column label="题干" prop="question" width="210">
          <template #default="{ row }">
            <div v-html="row.question" />
          </template>
        </el-table-column>
        <el-table-column label="录入时间" prop="addDate" width="210" />
        <el-table-column label="难度" prop="difficulty">
          <template #default="{ row }">
            <div v-if="row.difficulty === '1'">
              {{ '简单' }}
            </div>
            <div v-if="row.difficulty === '2'">
              {{ '普通' }}
            </div>
            <div v-if="row.difficulty === '3'">
              {{ '困难' }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="录入人" prop="creator" width="100" />
        <el-table-column label="审核状态" prop="chkState">
          <template #default="{ row }">
            <div v-if="row.chkState === 0">
              {{ '待审核' }}
            </div>
            <div v-if="row.chkState === 1">
              {{ '已通过' }}
            </div>
            <div v-if="row.chkState === 2">
              {{ '已拒绝' }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="审核意见" prop="chkRemarks" />
        <el-table-column label="审核人" prop="chkUser" width="100" />
        <el-table-column label="发布状态" prop="publishState">
          <template #default="{ row }">
            <div v-if="row.publishState === 0">
              {{ '已下架' }}
            </div>
            <div v-if="row.publishState === 1">
              {{ '已发布' }}
            </div>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="280" fixed="right">
          <template #default="{ row }">
            <div>
              <!-- 查看详情按钮 -->
              <el-button type="text" @click="DialogShow(row)"> 预览 </el-button>

              <!-- 审核按钮 -->
              <el-button
                type="text"
                :disabled="row.chkState !== 0"
                @click="resetQuestionCheck(row)"
              >
                审核
              </el-button>
              <!-- 修改按钮 -->
              <el-button
                type="text"
                :disabled="row.publishState == 1"
                @click="HandelReset(row)"
              >
                修改
              </el-button>
              <!-- 上架按钮 -->
              <el-button type="text" @click="resetPublishState(row)">
                {{ row.publishState == 1 ? '下架' : '上架' }}
              </el-button>
              <!-- 删除 -->
              <el-button
                type="text"
                :disabled="row.publishState == 1"
                @click="resetCompanyState(row)"
              >
                移出精选
              </el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="block" style="text-align: right; margin-top: 10px">
        <el-pagination
          :current-page="searFrom.page"
          :page-sizes="[5, 8, 10, 15]"
          :page-size="searFrom.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <!-- 详情组件 -->
    <inFoCmd
      :show-dialog="showDialog"
      :subject-data="subjectData"
      @add-success="getSubjectList"
      @closeDialog="closeDialog"
    />
    <!-- 审核组件 -->
    <reset-com
      :show-dialog="resetDialog"
      :subject-data="subjectData"
      @closeDialog="resetClose"
    />
  </div>
</template>

<script>
import {
  getStarQuestionListAPI,
  startQuestionsChoseAPI,
  resetPublishStateAPI
} from '@/api/basic'
import { delQuestionAPI } from '@/api/table'
import { getSubjectListAPI } from '@/api/subject'
// 导入组件
import { citys } from '@/components/MyCity'
import inFoCmd from './components/InfoCom.vue'
import ResetCom from './components/ResetCom.vue'
export default {
  name: 'Basic',
  components: { inFoCmd, ResetCom },
  data() {
    return {
      activeName: 'first',
      // 审核组件显示
      resetDialog: false,
      subjectData: {},
      // 详情组件显示
      showDialog: false,
      Area: [],
      cityQuery: [],
      areaQuery: [],
      province: '',
      city: '',
      area: '',
      // 搜索表单对象
      searFrom: {
        page: 1,
        pagesize: 5
        // keyword: '',
        // subjectID: ''
      },
      // 题库数据
      DatabaseList: [],
      total: 0,
      editForm: {},
      // 学科列表
      SubjectList: []
    }
  },
  watch: {
    // 监听省级
    province: function (newValue) {
      this.cityQuery = []
      this.shi = ''
      console.log(this.Area)
      this.Area.forEach((item) => {
        if (newValue.label === item.label) {
          this.cityQuery = item.children
          console.log('city', this.cityQuery)
        }
      })
    }
  },
  created() {
    this.getSubjectList()
    this.getDatabaseList()
    this.Area = citys
  },
  methods: {
    handleClick() {
      console.log(this.activeName)
      if (this.activeName === 'first') {
        this.handelMationClean()
      } else if (this.activeName === 'second') {
        this.searFrom.chkState = 0
        this.getDatabaseList()
      } else if (this.activeName === 'third') {
        this.searFrom.chkState = 1
        this.getDatabaseList()
      } else if (this.activeName === 'fourth') {
        this.searFrom.chkState = 2
        this.getDatabaseList()
      }
    },
    // 详情框打开时
    DialogShow(e) {
      console.log(e, '9')
      this.subjectData = e
      this.showDialog = true
    },
    // 关闭详情框
    closeDialog() {
      this.showDialog = false
    },
    // 审核框关闭
    resetClose() {
      this.resetDialog = false
      this.getDatabaseList()
    },
    // 试题审核打开
    async resetQuestionCheck(e) {
      this.subjectData = e
      this.resetDialog = true
    },
    // 修改试题
    async HandelReset(row) {
      try {
        const resp = await this.$router.push(`/list/card/?obj=${row.id}`)
        console.log(resp)
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    // 题库上架
    async resetPublishState(e) {
      try {
        if (e.publishState === 1) {
          await this.$confirm('确定下架吗？', '温馨提示', {
            type: 'warning'
          })
          await resetPublishStateAPI(e.id, 0)
          this.$message.success('🤖下架成功💩')
        } else {
          await resetPublishStateAPI(e.id, 1)
          this.$message.success('✨上架成功✨')
        }
      } catch (error) {
        console.log(error)
      } finally {
        this.getDatabaseList()
      }
    },
    // 获取精选试题列表
    async getDatabaseList() {
      const { data } = await getStarQuestionListAPI(this.searFrom)
      console.log(data)
      this.DatabaseList = data.items
      this.total = data.counts
    },
    // 移出精选
    async resetCompanyState(quesData) {
      console.log(quesData)
      try {
        await this.$confirm('确定移除精选吗？', '温馨提示', {
          type: 'warning'
        })
        await startQuestionsChoseAPI(quesData.id, 0)
        this.$message.success('移出成功🤡')
      } catch (error) {
        console.log(error)
      } finally {
        this.getDatabaseList()
      }
    },
    // 获取学科列表
    async getSubjectList() {
      const { data } = await getSubjectListAPI()
      console.log('学科列表', data)
      this.SubjectList = data.items
    },
    // 当条数改变时
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.searFrom.pagesize = val
      this.getDatabaseList()
    },
    // 当页数改变时
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.searFrom.page = val
      this.getDatabaseList()
    },
    // 删除试题
    async delCompany(question) {
      try {
        await this.$confirm('确定删除该试题吗？', '温馨提示', {
          type: 'warning'
        })
        await delQuestionAPI(question)
        this.$message.success('删除试题成功，丁真为你点赞🤪')
        // console.log(resp)
        this.getDatabaseList()
      } catch (error) {
        console.log(error)
      }
    },
    // 清空表单
    handelMationClean() {
      this.searFrom = {}
      this.city = this.province = ''
      this.getDatabaseList()
    }
  }
}
</script>

<style lang="scss" scoped>
.el-row {
  margin-bottom: 10px;
}
</style>

